package io.bdmc.modules.sys.service.impl;

import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.sys.dao.SysRoleMapper;
import io.bdmc.modules.sys.entity.SysRole;
import io.bdmc.modules.sys.service.SysRoleMenuService;
import io.bdmc.modules.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

/**
 * 角色
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年9月18日 上午9:45:12
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {
	@Autowired
	private SysRoleMapper roleMapper;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;


	@Override
	public SysRole queryObject(Long roleId) {
		return roleMapper.queryObject(roleId);
	}

	@Override
	@DataFilter(tableAlias = "d", user = false)
	public MyPage<SysRole> queryList(MyPage<SysRole> myPage){
		return roleMapper.queryList(myPage);
	}

	@Override
	@Transactional
	public void saveWithMenu(SysRole role) {
		role.setCreateTime(LocalDateTime.now());
		roleMapper.insert(role);
		
		//保存角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

	}

	@Override
	@Transactional
	public void updateWithMenu(SysRole role) {
		roleMapper.updateById(role);
		
		//更新角色与菜单关系
		sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());	
	}

	@Override
	public void deleteWithMenu(Long roleId) {
		roleMapper.deleteById(roleId);
		//todo
		//sysRoleMenuService.
	}


}
